package com.pagepub.expo.action.corp;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.pagepub.core.ftp.UploadImage;
import com.pagepub.core.ftp.UploadXml;
import com.pagepub.core.tag.message.HtmlMessageBox;
import com.pagepub.core.utils.Config;
import com.pagepub.core.utils.ExtendString;
import com.pagepub.core.utils.MyUtil;
import com.pagepub.core.utils.StringUtil;
import com.pagepub.expo.action.BaseAction;
import com.pagepub.expo.entity.AppDict;
import com.pagepub.expo.entity.CorpCatalog;
import com.pagepub.expo.entity.CorpCommpany;
import com.pagepub.expo.entity.CorpPublication;
import com.pagepub.expo.entity.CorpPublicationPage;
import com.pagepub.expo.manager.AppDictMng;
import com.pagepub.expo.manager.AppSeqMng;
import com.pagepub.expo.manager.CorpCatalogMng;
import com.pagepub.expo.manager.CorpCommpanyMng;
import com.pagepub.expo.manager.CorpPublicationMng;
import com.pagepub.expo.manager.CorpPublicationPageMng;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;

public class CorpPublicationPageAction extends BaseAction implements
		ModelDriven<CorpPublicationPage>, Preparable {
	private static final long serialVersionUID = 1L;

	@Autowired
	@Qualifier("corpPublicationPageMng")
	private CorpPublicationPageMng corpPublicationPageMng;

	@Autowired
	@Qualifier("corpPublicationMng")
	private CorpPublicationMng corpPublicationMng;

	
	@Autowired
	@Qualifier("corpCatalogMng")
	private CorpCatalogMng corpCatalogMng;

	@Autowired
	@Qualifier("appDictMng")
	private AppDictMng appDictMng;

	@Autowired
    @Qualifier("corpCommpanyMng")
	private CorpCommpanyMng corpCommpanyMng;
	
	
	@Autowired
	@Qualifier("appSeqMng")
	private AppSeqMng appSeqMng;

	private CorpPublicationPage corpPublicationPage;

	private File thumb;
	
	private File endpicture;


	private String thumbFileName; // 上传的文件名 (1.系统自动注入 2.变量命名有规则:
									// 前台对象名+"FileName")
	private String endpictureFileName; // 上传的文件名 (1.系统自动注入 2.变量命名有规则:
	// 前台对象名+"FileName")

	public CorpPublicationPage getModel() {
		return corpPublicationPage;
	}

	public void prepare() throws Exception {
		this.clearErrorsAndMessages();

		String actionName = ServletActionContext.getActionMapping().getName();
		if ("corpPublication_show".equals(actionName)
				|| "corpPublication_modify".equals(actionName)) {
			HttpServletRequest request = ServletActionContext.getRequest();
			if ("corpPublication_show".equals(actionName)) {
				corpPublicationPage = new CorpPublicationPage();

			} else {
				String id = request.getParameter("id");
				//corpPublicationPage = corpPublicationPageMng.f(new Long(id));
			}
		}

	}

	public String show() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String publicationid = ExtendString.nullToSpaceNoTrim(request
				.getParameter("publicationid"));
		request.setAttribute("publicationid",publicationid);
		request.setAttribute("fileformat", request.getParameter("fileformat"));
		request.setAttribute("leftfilesize",request.getParameter("leftfilesize"));
		request.setAttribute("filewh", request.getParameter("filewh"));
		return "newshow";
	}
	
	
//	public String batchUpload() {
//		HttpServletRequest request = ServletActionContext.getRequest();
//		int pageSize=10;
//		int fileSize=0;
//		Date date = new Date();
//		byte[] fileBytes = null;
//		String result="";
//		String filename="";
//		int width = 0, height = 0;
//	//	HttpSession session = request.getSession(true);
//	//	Memberuser memberuser = (Memberuser) session.getAttribute("memberuser");
//		String memberidtemp = ExtendString.nullToSpaceNoTrim(request
//				.getParameter("memberid"));// 会员ＩＤ
//		CorpCommpany corpCommpany = corpCommpanyMng.findById(StringUtil.stringToLong(memberidtemp));
//		
//		String corpId = corpCommpany.getId()+"";
//		// 本地临时路径
//		// 电子书期刊
//		String pubmagazinecount =corpPublicationMng.getPulicationName(corpId);
//
////		 电子书路径
//
//		String pubpath =  corpId+ "\\" + pubmagazinecount;
//		// 获取参数
//		// 上传封面图
//		UploadImage uploadimage = new UploadImage();
//		List files=(List)publicationPageBean.getTheFile();
//
//		InputStream fileIn=null;
//		JPEGImageDecoder decoder = null;
//		BufferedImage bufImage = null;
//		try {
//			fileIn = new FileInputStream(thumb);
//			fileSize = fileIn.available();// 取得文件尺寸
//			if (!fileIn.equals(null) && !fileIn.equals("")) {
//				decoder = JPEGCodec.createJPEGDecoder(fileIn);
//				bufImage = decoder.decodeAsBufferedImage();
//				width = bufImage.getWidth();
//				height = bufImage.getHeight();
//
//			}
//		} catch (Exception e) {
//			// TODO: handle exception
//			if (bufImage != null) {
//				bufImage.flush();
//			}
//			e.printStackTrace();
//		} finally {
//			if (decoder != null) {
//				decoder = null;
//			}
//			if (bufImage != null) {
//				bufImage.flush();
//			}
//			try {
//				if (fileIn != null) {
//					fileIn.close();
//				}
//			} catch (Exception e) {
//				fileIn = null;
//			}
//		}
//		// 默认信息
//		publication.setMemberid(memberuser.getMemberid());
//		publication.setCreatedate(date);
//		publication.setAreacode(memberuser.getAreacode());
//		publication.setPubpath(pubpath);
//		publication.setPubmagazinecount(TypeChange.stringToLong(pubmagazinecount));
//		// 以下为高级设置为空时调用默认设置
//		HtmlMessageBox hmb = new HtmlMessageBox();
//		hmb.setEnabledTimer(true);// 是否在页面有计时
//		hmb.setTimer(5);// 倒计时秒数,默认5秒
//		String httppath="http://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
//		hmb.setMessageOnPageType(HtmlMessageBox.MESSAGE_ON_PAGE_SPECIFY_URL);// .MESSAGE_ON_PAGE_PREACTION_URL);
//		hmb.setMessageOnPageURL(httppath+"/corp/publicationPage.do?m="
//				+ NavigationResults.LIST
//				+ "&corpmenuid="
//				+ ExtendString.nullToSpaceNoTrim(request
//						.getParameter("corpmenuid")) + "");
//		
//		String loginIp = request.getRemoteAddr();
//		Date sysDate = new Date();
//		
//		if (this.getPublication().savePublication(publication)) {
//			if(memberusertemp.getLoginfrom()==Config.SYSTEMUSER){
//				Staffuser staffUser = (Staffuser) session.getAttribute("staffUser");
//				 memberlog.setOpInfo("系统管理员:"+staffUser.getLoginusername()+"通过企业后台:"+memberusertemp.getCompanyname()+"：新增电子书内容信息成功!");			
//			}else if(memberusertemp.getLoginfrom()==Config.AGENTTEMUSER){
//				Agentuser agentuser = (Agentuser) session.getAttribute("agentuser");
//				memberlog.setOpInfo("代理商管理员:"+agentuser.getLoginname()+"通过企业后台:"+memberusertemp.getCompanyname()+"：新增电子书内容信息成功!");	
//			}else{
//				memberlog.setOpInfo("企业管理员:"+memberusertemp.getUsername()+"：新增电子书内容信息成功!");
//			}	
//			hmb.setMessage("新增成功！");// 提示信息
//			hmb.setForwardUrl(new String[] {
//					httppath+"/corp/publicationPage.do?m="
//							+ NavigationResults.LIST
//							+ "&corpmenuid="
//							+ ExtendString.nullToSpaceNoTrim(request
//									.getParameter("corpmenuid")) + "",
//									httppath+"/corp/publicationPage.do?m="
//							+ NavigationResults.NEWSHOW
//							+ "&corpmenuid="
//							+ ExtendString.nullToSpaceNoTrim(request
//									.getParameter("corpmenuid")) + "" });// 提示信息页面设置链接到指定位置
//
//
//			hmb.setLinkMsg(new String[] { "到列表", "继续新增" });// 链接到指定位置的提示信息
//		}
//		return "success";
//	}
	

	public String add() throws Exception {
		String display = "list";
		String resultFilename;
		int width = 0, height = 0;
		int fileSize=0;
		int backFileSize=0;
		HttpServletRequest request = ServletActionContext.getRequest();
		String root = ServletActionContext.getServletContext().getRealPath(
		"/expo/pub");
		
		return display;
	}


	public File getEndpicture() {
		return endpicture;
	}

	public void setEndpicture(File endpicture) {
		this.endpicture = endpicture;
	}

	public String getEndpictureFileName() {
		return endpictureFileName;
	}

	public void setEndpictureFileName(String endpictureFileName) {
		this.endpictureFileName = endpictureFileName;
	}

	public File getThumb() {
		return thumb;
	}

	public void setThumb(File thumb) {
		this.thumb = thumb;
	}

	public String getThumbFileName() {
		return thumbFileName;
	}

	public void setThumbFileName(String thumbFileName) {
		this.thumbFileName = thumbFileName;
	}

}
